<?php

require_once "Connect.php";
require_once "Resize.php";

function resizeHoang($file, $width, $height, $file_name, $des) {
    require_once "lib/class.resize.php";
    $re = new resizes;
    $re->load($file);
    $re->resize($width, $height);
    $re->save($des . $file_name);
}

class Mau extends Connect {

    function getListMau() {
        $sql = "SELECT * FROM mau ORDER BY mau_id ASC ";
        $rs = mysql_query($sql) or die(mysql_error());
        return $rs;
    }

    function getDetailMau($mau_id) {
        $sql = "SELECT * FROM mau WHERE mau_id =$mau_id ";
        $rs = mysql_query($sql) or die(mysql_error());
        return $rs;
    }

    function Mau_ChiTiet($mau_id) {
        $sql = "SELECT * FROM mau WHERE mau_id = $mau_id";
        $rs = mysql_query($sql) or die(mysql_error());
        return $rs;
    }

    function Mau_Them(&$loi) {

        $thanhcong = true;

        $ten = $this->processData($_POST['ten']);

        $hinh = $this->processData($_POST['hinh']);

        $arrTmp = explode("/", $hinh);
        $sopt = count($arrTmp);
        echo $file_name = $arrTmp[$sopt - 1];



        $urlResize = "../" . str_replace($file_name, "small/", $hinh);

        if (!is_dir($urlResize)) {
            mkdir($urlResize, 0700, true);
        } else {
            echo "da co thu muc";
        }

        resizeHoang(
                "../" . $hinh, 25, 25, $file_name, $urlResize
        );


        if ($thanhcong == false) {
            return $thanhcong;
        } else {
            $sql = "INSERT INTO mau
					VALUES(NULL,'$ten','$hinh')";
            mysql_query($sql) or die(mysql_error() . $sql);
        }
        return $thanhcong;
    }

    function Mau_Sua($mau_id, &$loi) {
        settype($mau_id, "int");
        $thanhcong = true;

        $ten = $this->processData($_POST['ten']);
        $hinh = $this->processData($_POST['hinh']);


        if ($thanhcong == false) {
            return $thanhcong;
        } else {
            $sql = "UPDATE mau
					SET ten = '$ten',hinh = '$hinh' WHERE mau_id = $mau_id";
            mysql_query($sql) or die(mysql_error() . $sql);
        }
        return $thanhcong;
    }

}

?>